<?php

namespace App\Admin\Controllers;

use App\Models\Device;
use App\Models\HeartLog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class HeartLogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '心跳';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new HeartLog());
        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', __('Id'));
        $grid->column('device_id', __('Device id'));
//        $grid->column('device_code', __('Device code'));
        $grid->column('soc', __('Soc'));
        $grid->column('card_list', __('Card list'));
        $grid->column('ip', __('Ip'));

        $grid->column('created_at', __('Created at'))->sortable();;
        $grid->column('updated_at', __('Updated at'))->sortable();;
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->equal('device_id', __('Device id'))->select(Device::all()->pluck('full_tag','device_id'));
            $filter->between('created_at', __('Created at'))->datetime();;

        });
        $grid->disableCreateButton();

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();

            // 去掉查看
//            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(HeartLog::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('local_device_id', __('Local device id'));
        $show->field('device_id', __('Device id'));
        $show->field('device_code', __('Device code'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('soc', __('Soc'));
        $show->field('card_list', __('Card list'));
        $show->field('ip', __('Ip'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new HeartLog());
        $models = Device::all();
        $list = [];
        foreach ($models as $model) {
            $list[$model->id] = "$model->name $model->code";

        }
        $form->select('device_id', __('Device id'))->options($list)->required();
        $form->slider('soc', __('Soc'))->options([
            'max'       => 100,
            'min'       => 0,
            'step'      => 10,
            'postfix'   => '百分之'
        ]);
        $form->textarea('card_list', __('Card list'))->required();
        $form->ip('ip', __('Ip'))->required();

        return $form;
    }
}
